<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ page import="com.hzqy.commons.utils.ConstantUtils" %>
<%@ taglib prefix="limits" uri="/WEB-INF/tld/limits.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>zTree</title>
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/defalut.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/public.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/ddspstyle.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/icon.css">
<script type="text/javascript" src="../static/js/jquery.min.js"></script>
<script type="text/javascript" src="../static/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="../static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../static/js/jquery.validate.js"></script>
<script type="text/javascript" src="../static/js/index.js"></script>
<link rel="stylesheet" type="text/css"
	href="../static/zTree/css/zTree.css">
<link rel="stylesheet" type="text/css"
	href="../static/zTree/css/zTreeStyle/zTreeStyle.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/default/easyui.css">
<script type="text/javascript"
	src="../static/zTree/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="../static/zTree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript"
	src="../static/zTree/js/jquery.ztree.exedit.js"></script>
	<script type="text/javascript" charset="utf-8" src="../static/js/Base64.js"></script>
	<script type="text/javascript" charset="utf-8" src="../static/js/beforeData.js"></script>
	<%
	String []nonceObj=ConstantUtils.getNonceObject();
	%>
<script type="text/javascript">
var isAdd = true;
var setting = {
		view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true,
			editNameSelectAll: true,
			showRemoveBtn: false,
			showRenameBtn: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeDrag: beforeDrag,
			onClick:zTreeOnClick
		}
	};
	//禁止拖拽
	function beforeDrag(treeId, treeNodes) {
		return false;
	}
	//点击事件
	function zTreeOnClick(event,treeId,treeNode){
		if(treeNode.id !=0)
			editInit(treeNode);
	}
	function nodeRemove(treeId, treeNode) {
		$.messager.confirm("操作提示","您确定要删除"+treeNode.name+" 吗？",function(sure){
			if(sure) {
				$.ajax({
					url:"del_resource.do?f_pr_id="+treeNode.id+'&stime=<%=nonceObj[0]%>&sign=<%=nonceObj[1]%>',
					success:function(data) {
						data = JSON.parse(data);
						if(data.success){
							$.messager.alert("操作提示","操作成功!","info",function(){
								window.location.reload();
							});
						} else {
							$.messager.alert("操作提示",data.result,"error");
						}
						window.location.reload();
					}
				});
			}
		});
		return false;
	}
	function addHoverDom(treeId, treeNode) {
		if(!treeNode.isParent) {
			var sObj2 = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#removeBtn_"+treeNode.tId).length>0) return;
			var removeStr = "<limits:uiAuth url='del_resource.do'><span class='button remove' id='removeBtn_" + treeNode.tId
				+ "' title='remove node' onfocus='this.blur();'></span></limits:uiAuth>";
			sObj2.after(removeStr);
			var btn = $("#removeBtn_"+treeNode.tId);
			if (btn) btn.bind("click", function(event){
				nodeRemove(treeId, treeNode);
				event.stopPropagation();
			});
		}
		
		var sObj1 = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#editBtn_"+treeNode.tId).length>0) return;
		var editStr = "<limits:uiAuth url='update_resource.do'><span class='button edit' id='editBtn_" + treeNode.tId
			+ "' title='edit node' onfocus='this.blur();'></span></limits:uiAuth>";
		sObj1.after(editStr);
		var btn = $("#editBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(event){
			editInit(treeNode);
			event.stopPropagation();
		});
		
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = "<limits:uiAuth url='resource_add.do'><span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='add node' onfocus='this.blur();'></span></limits:uiAuth>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(event){
			addInit(treeNode);
			event.stopPropagation();
		});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.tId).unbind().remove();
		$("#editBtn_"+treeNode.tId).unbind().remove();
		$("#removeBtn_"+treeNode.tId).unbind().remove();
	};
	function selectAll() {
		var zTree = $.fn.zTree.getZTreeObj("ztree");
		zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
	}
	
	$(document).ready(function(){
		$("#right").hide();
		$.ajax({
			url:"resource_list.do?f_pr_id=0",
			success:function(data){
				zNodes = JSON.parse(data);
				var root = {id:"0",name:"根目录",open:true};
				zNodes.push(root);
				$.fn.zTree.init($("#ztree"), setting, zNodes);
				$("#selectAll").bind("click", selectAll);
			}
		});
	});
	$(function(){
		$("#contentForm").validate({
			rules: {
				f_pr_name: {
					required: true,
					minlength: 3,
					maxlength: 50
				},
				f_pr_url:{
					required:true
				}
			},
			messages: {
				f_pr_name: {
					required: "请输入资源名称",
					minlength: "资源名称不能少于3位",
					maxlength: "资源名称不能超过50位"
				},
				f_pr_url:{
					required: "请输入资源路径"
				}
			},
			
		});
	});
</script>
</head>
<body style="background: #FFFFFF;">
	<div id="left"
		style="width: 30%; overflow: auto; float: left; background: #d2e0f2;">
		<ul id="ztree" class="ztree"></ul>
	</div>
	<div id="right" style="width: 70%; overflow: auto; float: right">
		<!-- <div class="barnavtop">您所在的位置：资源管理 &gt; 资源新增</div> -->
		<div id="container">
			<div class="editspace">
				<form id="contentForm" class="cmxform" enctype="multipart/form-data"
					name="formName">
					<input type="hidden" id="f_pr_parentId" name="f_pr_parentId"
						value="" /> <input type="hidden" id="f_pr_id" name="f_pr_id"
						value="" />
					<legend>资源新增</legend>
					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">上级目录：</td>
									<td id="parentName" width="70%"></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">资源名称：</td>
									<td width="70%"><input id="f_pr_name" type="text"
										name="f_pr_name" value="" /></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">资源路径：</td>
									<td width="70%"><input id="f_pr_url" type="text"
										name="f_pr_url" value="" /></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">资源描述：</td>
									<td width="70%"><textarea id="f_pr_desc" name="f_pr_desc"
											style="resize: none; width: 150px;" rows="4" cols="20">
										</textarea>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">资源状态：</td>
									<td width="70%"><select name="f_pr_status"
										id="f_pr_status" style="width: 150px;">
											<option value="1">有效</option>
											<option value="0">无效</option>
									</select></td>
								</tr>
							</table>
						</div>
					</fieldset>
					<input type="hidden" name="stime" value="<%=nonceObj[0]%>"/>
					<input type="hidden" name="sign" value="<%=nonceObj[1]%>"/>
				</form>
			</div>
			<div class="toolbar" style="text-align: center;">
				<a id="easyui-linkbutton" class="easyui-linkbutton"
					data-options="iconCls:'icon-ok'" href="javascript:void(0);"
					onclick="_submitFm()">保存</a> <a href="javascript:void(0);"
					data-options="iconCls:'icon-cancel'" class="easyui-linkbutton"
					onclick="javascript:parent.$('#dlg').dialog('close')">取消</a>
			</div>
		</div>
		<!--主体 结束-->
	</div>
</body>
<script type="text/javascript">
function _submitFm() {
	 if($("#contentForm").validate().form()) {
		if(isAdd){
			app_ajax('resource_add.do', $("#contentForm").serialize(), function(data){
				if(data.success) {
					$.messager.progress('close');
					$.messager.alert('提示', '提交成功！', 'info', function() {
						parent.$('#dlg').dialog('close');
						parent.gotoSearch($('#easyui-treegrid'));
						window.location.reload();
					});
				} else {
					$.messager.alert('提示', data.result, 'info', function() {
						$.messager.progress('close');
					});
				}
			});	
		} else {
			app_ajax('update_resource.do', $("#contentForm").serialize(), function(data){
				if(data.success) {
					$.messager.progress('close');
					$.messager.alert('提示', '提交成功！', 'info', function() {
						parent.$('#dlg').dialog('close');
						parent.gotoSearch($('#easyui-treegrid'));
					});
				} else {
					$.messager.alert('提示', data.result, 'info', function() {
						$.messager.progress('close');
					});
				}
			});
		}
		
	 }
}
function addInit(treeNode) {
	isAdd = true;
	$("legend").html("资源新增");
	$("#parentName").html(treeNode.name);
	$("#f_pr_parentId").val(treeNode.id);
	$("#f_pr_name").val("");
	$("#f_pr_url").val("");
	$("#f_pr_desc").val("");
	$("#f_pr_status").val(1);
	$("#f_pr_id").val(0);
	$("#right").show();
}
function editInit(treeNode) {
	isAdd = false;
	$("legend").html("资源修改");
	$.ajax({
		url:"select_resource.do?f_pr_id="+treeNode.id,
		success:function(data){
			$("#parentName").html(treeNode.getParentNode().name);
			data=JSON.parse(data);
			$("#f_pr_id").val(data.f_pr_id);
			$("#f_pr_parentId").val(treeNode.getParentNode().id);
			$("#f_pr_name").val(data.f_pr_name);
			$("#f_pr_url").val(data.f_pr_url);
			$("#f_pr_desc").val(data.f_pr_desc);
			$("#f_pr_status").val(data.f_pr_status);
			$("#right").show();
			beforeData(data, treeNode.id, "", "../beforeData.do");
		}
	});
}
</script>
</html>